﻿//69.x的平方根
class Solution {
public:
    int mySqrt(int x) 
    {
        if (x < 1)
        {
            return 0;
        }

        int left = 1, right = x / 2;
        while (left < right)
        {
            int mid = left + (right - left + 1) / 2;
            if (mid > x / mid)
            {
                right = mid - 1;
            }
            else
            {
                left = mid;
            }
        }
        return left;
    }
};

//35.搜索插入位置
class Solution {
public:
    int searchInsert(vector<int>& nums, int target)
    {
        int left = 0, right = nums.size() - 1;
        while (left < right)
        {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target)
            {
                left = mid + 1;
            }
            else
            {
                right = mid;
            }
        }

        if (nums[right] < target) return right + 1;
        return right;
    }
};

//746.使用最小花费爬楼梯
//方法一，dp[i]表示到达第 i 阶台阶需要花费的最小花费
class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost)
    {
        int size = cost.size();
        vector<int> dp(size + 1);
        for (int i = 2; i <= size; i++)
        {
            dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
        }
        return dp[size];
    }
};
//方法二，dp[i]表示从第 i 阶台阶到楼顶需要花费的最小花费
class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost)
    {
        int size = cost.size();
        vector<int> dp(size);
        dp[size - 1] = cost[size - 1], dp[size - 2] = cost[size - 2];
        for (int i = size - 3; i >= 0; i--)
        {
            dp[i] = min(dp[i + 1] + cost[i], dp[i + 2] + cost[i]);
        }
        return min(dp[0], dp[1]);
    }
};